﻿using Kingdee.BOS.Core.Bill.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Core.Objects.BillCodeRule;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Orm.Metadata.DataEntity;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.ServiceHelper.BillCodeRule;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace PMAF.K3Cloud.SCM.Bill.BusinessPlugIn
{
    [HotUpdate]
    [Description("采购订单产生流水号")]
    public class PurchaseOrder : AbstractBillPlugIn
    {
        
        public override void EntryBarItemClick(BarItemClickEventArgs e)
        {
            base.BarItemClick(e);

            DynamicObject dataObject = View.Model.DataObject;

            if (e.BarItemKey.EqualsIgnoreCase("PMAF_PurchaseBtn"))
            {
                if (dataObject != null)
                {
                    DynamicObject supplier = (DynamicObject)dataObject["SupplierId"];
                    DynamicObject purchaser = (DynamicObject)dataObject["PurchaserId"];
                    if (purchaser == null)
                    {
                        e.Cancel = true;
                        View.ShowWarnningMessage("请填写采购员信息");
                        return;
                    }
                    DateTime createDate = (DateTime)dataObject["CreateDate"];
                    int rows = View.Model.GetEntryRowCount("POOrderEntry");
                    DynamicObjectCollection poEntries = dataObject["POOrderEntry"] as DynamicObjectCollection;
                    StringBuilder stringBuilder = new StringBuilder();
                    int index = 0;
                    foreach (var poEntry in poEntries)
                    {
                        var material = (DynamicObject)poEntry["MaterialId"];
                        var purchaseNo = poEntry["F_PMAF_PurchaseSN"];
                        if (material != null)
                        {
                            if(purchaseNo != null)  //不为空
                            {
                                if (purchaseNo.ToString()==" " || purchaseNo.ToString() == "") //等于空格（暂存产生）
                                {
                                    goto emptyValue;
                                }
                                else   //有完整流水号
                                {
                                    goto retry;
                                }
                            }
                            emptyValue:
                            Dictionary<string, object> options = new Dictionary<string, object>
                            {
                                ["CodeTime"] = 1,
                                ["UpdateMaxNum"] = 1
                            };
                            string formId = "PMAF_PurchaseSn";
                            FormMetadata meta = (FormMetadata)MetaDataServiceHelper.Load(Context, formId);
                            BusinessInfo businessInfo = meta.BusinessInfo;
                            DynamicObjectType dynamicObjectType = businessInfo.GetDynamicObjectType();
                            DynamicObject dynamicObject = new DynamicObject(dynamicObjectType);
                            dynamicObject["F_PMAF_SupplierID"] = supplier;
                            dynamicObject["F_PMAF_EmpID"] = purchaser;
                            dynamicObject["F_PMAF_MaterialID"] = material;
                            dynamicObject["F_PMAF_CreateDate_tzk"] = createDate;
                            string ruleId = "66ac51b4f28f38";   //66b1d1da24b27f（中）    66ac51b4f28f38(本)
                            GenerateBillNoSceneEnum generateBillNoSceneEnum = new GenerateBillNoSceneEnum { };
                            var billNos = BillNoRuleServiceHelper
                                .GetBillNo(Context, businessInfo, new[] { dynamicObject }, generateBillNoSceneEnum, options, ruleId);
                            var purchaseSn = string.Join(",", billNos.Select(o => o.BillNo));
                            //更新流水号
                            View.Model.SetValue("F_PMAF_PurchaseSN", purchaseSn, index);
                            //更新批号
                            View.Model.SetValue("FLot", purchaseSn, index);
                            View.UpdateView("F_PMAF_PurchaseSN");
                            retry:
                            index++;
                        }
                    }
                }
            }
        }
    }
}
